package com.meet.controller;

import com.meet.api.dto.request.MeetActivityRequestDTO;
import com.meet.api.dto.response.MeetActivityResponseDTO;
import com.meet.bizservice.ActivityService;
import com.meet.util.JwtUtil;
import com.meet.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/activities")
@CrossOrigin
@Validated
public class MeetActivityController {

    @Autowired
    private ActivityService activityService;

    @GetMapping("/list")
    public Result<?> getActivities(@RequestParam(defaultValue = "1") @Min(1) int page,
                                   @RequestParam(defaultValue = "10") @Min(1) int limit,
                                   @RequestParam(required = false) String location,
                                   @RequestParam(required = false) String category,
                                   @RequestParam(required = false) String timeFilter,
                                   @RequestParam(required = false) String keyword) {

        return activityService.getActivities(page, limit, location, category, timeFilter, keyword);
    }

    @GetMapping("/{activityId}")
    public Result<MeetActivityResponseDTO> getActivityDetail(@PathVariable @NotBlank String activityId) {

        return activityService.getActivityDetail(activityId);
    }

    @PostMapping
    public Result<?> createActivity(@RequestBody @Validated MeetActivityRequestDTO activity) {

        return activityService.createActivity(activity);
    }

    @PostMapping("/{activityId}/register")
    public Result<?> registerForActivity(@PathVariable @NotBlank String activityId,
                                         HttpServletRequest request) {
        // 从JWT token中解析用户ID
        String authHeader = request.getHeader("Authorization");
        if (authHeader == null || !authHeader.startsWith("Bearer ")) {
            return Result.error(401, "未提供有效的认证信息", "UNAUTHORIZED");
        }

        String userId = JwtUtil.getUserIdFromToken(authHeader);
        if (userId == null) {
            return Result.error(401, "认证信息无效", "INVALID_TOKEN");
        }

        return activityService.registerForActivity(activityId, userId);
    }

    @DeleteMapping("/{activityId}/register")
    public Result<?> cancelRegistration(@PathVariable @NotBlank String activityId,
                                        HttpServletRequest request) {
        // 从JWT token中解析用户ID
        String authHeader = request.getHeader("Authorization");
        if (authHeader == null || !authHeader.startsWith("Bearer ")) {
            return Result.error(401, "未提供有效的认证信息", "UNAUTHORIZED");
        }

        String userId = JwtUtil.getUserIdFromToken(authHeader);
        if (userId == null) {
            return Result.error(401, "认证信息无效", "INVALID_TOKEN");
        }

        return activityService.cancelRegistration(activityId, userId);
    }

}
